package com.park.controller;

import com.park.bean.Tickets;
import com.park.bean.TicketsCap;
import com.park.common.CommonResult;
import com.park.dao.ITicketsCapDao;
import com.park.service.ITicketsCapService;
import com.park.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class TicketsCapController {
    @Autowired
    ITicketsCapService ticketsCapService;

    //展示和查询数据
    @GetMapping("tss")
    public CommonResult getTicketsCap(TicketsCap ticketsCap, Page page){
        //System.out.println("getTicketsCap---------");
        List<TicketsCap> res=ticketsCapService.getTicketsCap(ticketsCap,page);
        int count=ticketsCapService.getCount(ticketsCap);
        HashMap map=new HashMap();
        map.put("res",res);
        map.put("count",count);
        //System.out.println(res);
        return CommonResult.success(map);
    }
    @GetMapping("tssauto")
    public CommonResult getTicketsId(TicketsCap ticketsCap){
        List<Integer> list=ticketsCapService.getTicketsId(ticketsCap);
        return CommonResult.success(list);
    }

    @GetMapping("gettname")
    public CommonResult getTicketsById(TicketsCap ticketsCap){
        List<Tickets> list=ticketsCapService.getTicketsByName(ticketsCap.getTicketsName());
        return CommonResult.success(list);
    }


    @GetMapping("tattracts")
    public CommonResult getAttractsName(TicketsCap ticketsCap){
        List<String> list=ticketsCapService.getAttractsName(ticketsCap);
        return CommonResult.success(list);
    }

    @GetMapping("tid")
    public CommonResult checkTid(TicketsCap ticketsCap){
        boolean bool= ticketsCapService.checkTid(ticketsCap.getTicketsId());
        if(bool){
            return CommonResult.success(200,"存在id");
        }else{
            return CommonResult.fail();
        }
    }

    @GetMapping("tname")
    public CommonResult checkTname(TicketsCap ticketsCap){
        boolean bool=ticketsCapService.checkTname(ticketsCap.getTicketsName());
        if(bool){
            return CommonResult.success(200,"存在票名");
        }else{
            return CommonResult.fail();
        }
    }

    @GetMapping("aname")
    public CommonResult checkAname(TicketsCap ticketsCap){
        boolean bool=ticketsCapService.checkAname(ticketsCap.getAttractsName());
        if(bool){
            return CommonResult.success(200,"存在设施名");
        }else{
            return CommonResult.fail();
        }
    }

    //添加数据
    @PostMapping("tss")
    public CommonResult postTicketsCap(@RequestBody TicketsCap ticketsCap){
        boolean bool=ticketsCapService.duplicateCheck(ticketsCap);
        if(bool){
            int count=ticketsCapService.postTicketsCap(ticketsCap);
            if(count>0){
                return CommonResult.success(ticketsCap);
            }else{
                return CommonResult.fail();
            }
        }else {
            return CommonResult.fail(409,"添加的数据已经存在");
        }

    }

    //删除数据
    @DeleteMapping("tss/{id}")
    public CommonResult delTicketsCap(@PathVariable int id){
        int count= ticketsCapService.delTicketsCap(id);
        if(count>0){
            return CommonResult.success();
        }else{
            return CommonResult.fail();
        }
    }

    //修改数据
    @PutMapping("tss")
    public CommonResult editTicketsCap(@RequestBody TicketsCap ticketsCap){
        int count=ticketsCapService.editTicketsCap(ticketsCap);
        if(count>0){
            return CommonResult.success(ticketsCap);
        }else{
            return CommonResult.fail();
        }
    }


}
